/**
*
* University of Illinois/NCSA
* Open Source License
*
* Copyright (c) 2008, 2010 NCSA.  All rights reserved.
*
* Developed by:
* The Automated Learning Group
* University of Illinois at Urbana-Champaign
* http://www.seasr.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal with the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimers.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
*
* Neither the names of The Automated Learning Group, University of
* Illinois at Urbana-Champaign, nor the names of its contributors may
* be used to endorse or promote products derived from this Software
* without specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
*
*/

package org.seasr.meandre.components.vis.html;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.meandre.annotations.Component;
import org.meandre.annotations.ComponentInput;
import org.meandre.annotations.ComponentOutput;
import org.meandre.annotations.ComponentProperty;
import org.meandre.annotations.Component.FiringPolicy;
import org.meandre.annotations.Component.Licenses;
import org.meandre.core.ComponentContext;
import org.meandre.core.ComponentContextProperties;
import org.seasr.datatypes.core.BasicDataTypesTools;
import org.seasr.datatypes.core.DataTypeParser;
import org.seasr.datatypes.core.Names;
import org.seasr.meandre.components.abstracts.AbstractExecutableComponent;

/**
 * @author Lily Dong
 * @author Boris Capitanu
 * @author Devin Griffiths
 */

@Component(
        creator = "Boris Capitanu",
        description = "Generates HTML frames based on the input data." +
           		      "The encoding of the data is specified via the " + Names.PROP_ENCODING + " property. " +
           		      "Supported MIME types: 'text/plain', 'image/<EXT>' (where <EXT> is one of the standard " +
           		      "image types; ex: jpg, png...)",
        firingPolicy = FiringPolicy.any,
        name = "HTML Frame Maker 2",
        rights = Licenses.UofINCSA,
        tags = "html",
        baseURL = "meandre://seasr.org/components/foundry/",
        dependency = {"protobuf-java-2.2.0.jar","commons-lang-2.4.jar"}
)
public class HTMLFrameMaker2 extends AbstractExecutableComponent {

    //------------------------------ INPUTS ------------------------------------------------------

	@ComponentInput(
            name = Names.PORT_RAW_DATA,
            description = "Raw data encoded in one of the supported encoding types." +
            "<br>For text mime type:" +
            "<br>TYPE: java.lang.String" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings" +
            "<br>TYPE: byte[]" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Bytes" +
            "<br>TYPE: java.lang.Object" +
            "<br><br>For image mime type:" +
            "<br>TYPE: byte[]"
    )
    protected static final String IN_RAW_DATA = Names.PORT_RAW_DATA;

	@ComponentInput(
            name = "rawData1",
            description = "Raw data encoded in one of the supported encoding types." +
            "<br>For text mime type:" +
            "<br>TYPE: java.lang.String" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings" +
            "<br>TYPE: byte[]" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Bytes" +
            "<br>TYPE: java.lang.Object" +
            "<br><br>For image mime type:" +
            "<br>TYPE: byte[]"
    )
    protected static String RAW_DATA_1 = "rawData1";

	@ComponentInput(
            name = "rawData2",
            description = "Raw data encoded in one of the supported encoding types." +
            "<br>For text mime type:" +
            "<br>TYPE: java.lang.String" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings" +
            "<br>TYPE: byte[]" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Bytes" +
            "<br>TYPE: java.lang.Object" +
            "<br><br>For image mime type:" +
            "<br>TYPE: byte[]"
    )
    protected static String RAW_DATA_2 = "rawData2";

	@ComponentInput(
            name = "rawData3",
            description = "Raw data encoded in one of the supported encoding types." +
            "<br>For text mime type:" +
            "<br>TYPE: java.lang.String" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings" +
            "<br>TYPE: byte[]" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Bytes" +
            "<br>TYPE: java.lang.Object" +
            "<br><br>For image mime type:" +
            "<br>TYPE: byte[]"
    )
    protected static String RAW_DATA_3 = "rawData3";

	@ComponentInput(
            name = "rawData4",
            description = "Raw data encoded in one of the supported encoding types." +
            "<br>For text mime type:" +
            "<br>TYPE: java.lang.String" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings" +
            "<br>TYPE: byte[]" +
            "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Bytes" +
            "<br>TYPE: java.lang.Object" +
            "<br><br>For image mime type:" +
            "<br>TYPE: byte[]"
    )
    protected static String RAW_DATA_4 = "rawData4";


    //------------------------------ OUTPUTS -----------------------------------------------------

    @ComponentOutput(
            name = Names.PORT_HTML,
            description = "The HTML fragment wrapping the input data." +
                "<br>TYPE: org.seasr.datatypes.BasicDataTypes.Strings"
    )
    protected static final String OUT_HTML = Names.PORT_HTML;

    //------------------------------ PROPERTIES --------------------------------------------------

    @ComponentProperty(
            defaultValue = "text/plain",
            description = "Specifies the MIME encoding of the input data.",
            name = Names.PROP_ENCODING
    )
    protected static final String PROP_ENCODING = Names.PROP_ENCODING;

    @ComponentProperty(
            defaultValue = "",
            description = "Specifies the ID attached to the HTML fragment.",
            name = Names.PROP_ID
    )
    protected static final String PROP_ID = Names.PROP_ID;

    @ComponentProperty(
            defaultValue = "",
            description = "Specifies a style attribute for the HTML fragment.",
            name = Names.PROP_CSS
    )
    protected static final String PROP_CSS = Names.PROP_CSS;

    @ComponentProperty(
            defaultValue = "1",
            description = "Specifies the number of frames -- max 4.",
            name = "frames"
    )
    protected static final String PROP_FRAMES = "frames";

    @ComponentProperty(
            defaultValue = "vertical",
            description = "Specifies the orientation of the frames.",
            name = "orientation"
    )
    protected static final String PROP_ORIENTATION = "orientation";

    //--------------------------------------------------------------------------------------------


    private String _mimeType;
    private String _id;
    private String _css;
    private int _frames;
    private String _orientation;






    //--------------------------------------------------------------------------------------------

    
    
    @Override
    public void initializeCallBack(ComponentContextProperties ccp) throws Exception {
        _mimeType = ccp.getProperty(PROP_ENCODING).toLowerCase();

        _id = ccp.getProperty(PROP_ID);
        if (_id.trim().length() == 0) _id = null;

        _css = ccp.getProperty(PROP_CSS);
        if (_css.trim().length() == 0) _css = null;

        _frames = Integer.parseInt(ccp.getProperty(PROP_FRAMES));

        _orientation = ccp.getProperty(PROP_ORIENTATION).toLowerCase();


    }
    List<Object> objects = new ArrayList<Object>();

    

    @Override
    public void executeCallBack(ComponentContext cc) throws Exception {
        
    	if (objects.size() < _frames) {
    		for (int i = 0 ; i < 4 ; i++){
    		 
    		Object data =  DataTypeParser.parseAsString(cc.getDataComponentFromInput(IN_RAW_DATA))[0];
            objects.add(data);
            return;
    		}
    	}

    	
    	if (_mimeType.startsWith("text")) {

    			if (cc.isInputAvailable(RAW_DATA_1)) {
    			Object data =  DataTypeParser.parseAsString(cc.getDataComponentFromInput(RAW_DATA_1))[0];
                objects.add(data);
                }

    			if (cc.isInputAvailable(RAW_DATA_2)) {
        			Object data =  DataTypeParser.parseAsString(cc.getDataComponentFromInput(RAW_DATA_2))[0];
                    objects.add(data);

        		}

    			if (cc.isInputAvailable(RAW_DATA_3)) {
        			Object data =  DataTypeParser.parseAsString(cc.getDataComponentFromInput(RAW_DATA_3))[0];
                    objects.add(data);
                    }

    			if (cc.isInputAvailable(RAW_DATA_4)) {
        			Object data =  DataTypeParser.parseAsString(cc.getDataComponentFromInput(RAW_DATA_4))[0];
                    objects.add(data);
                    }
    			int count = objects.size();
    		if (count == _frames){
    	//		String htmlTextFrames = org.seasr.meandre.support.generic.html.HTMLFrameMaker.makeHtmlTextFrame(objects, cc.getPublicResourcesDirectory()
         //       		+ File.separator + "devin", "/public/resources/devin/", _orientation, _id, _css);
                String htmlTextFrames = org.seasr.meandre.support.generic.html.HTMLFrameMaker.makeHtmlTextFrame(objects, _orientation, _id, _css);
            console.fine("Pushing out text frames: " + htmlTextFrames);
            cc.pushDataComponentToOutput(OUT_HTML, BasicDataTypesTools.stringToStrings(htmlTextFrames));
    		}
    	}

    	else




        if (_mimeType.startsWith("image")) {
            for (int i = 1; i <= _frames; i++) {
                Object data =  cc.getDataComponentFromInput("RAW_DATA" + i);
                objects.add(data);
            }
        //    String htmlImageFrames = org.seasr.meandre.support.generic.html.HTMLFrameMaker.makeHtmlImageFrame(objects, cc.getPublicResourcesDirectory()
        //    		+ File.separator + "devin", "/public/resources/devin/", _id, _css, _orientation);
            String htmlImageFrames = org.seasr.meandre.support.generic.html.HTMLFrameMaker.makeHtmlImageFrame(objects, cc.getPublicResourcesDirectory()
                  		+ File.separator + "devin" + "/public/resources/devin/", _id, _css, _orientation);
                        console.fine("Pushing out image frames: " + htmlImageFrames);
            cc.pushDataComponentToOutput(OUT_HTML, BasicDataTypesTools.stringToStrings(htmlImageFrames));
        }

        else
            throw new UnsupportedEncodingException("Unknown MIME type specified: " + _mimeType);
    }

    @Override
    public void disposeCallBack(ComponentContextProperties ccp) throws Exception {

    }
}
